﻿#include "gameworld.h"
#include "mycontactresultcallback.h"
#include<btBulletCollisionCommon.h>
#include<btBulletDynamicsCommon.h>
#include<btosgmycar.h>
#pragma execution_character_set("utf-8")
GameWorld* GameWorld::_obj=nullptr;

GameWorld::GameWorld()
{
    this->myWorld=new btosgWorld;
    //设置重力方向

    btosgVec3 up(0,0,1.);
    //模拟真实的重力场景
    btosgVec3 gravity=up*-9.8;
    //设置重力方向
    this->myWorld->dynamic->setGravity(gravity);
    //设置车子方向
//    this->myVehicle=new btosgVehicle(myWorld);
      this->myVehicle=new btosgMyCar(myWorld);
    //重点，这只自行车的位置


//    this->myVehicle->setPosition(btosgVec3(up*3));
    this->myVehicle->setPosition(btosgVec3(up));



    //名字
    myVehicle->setName("Vehicle");
    //质量
    myVehicle->setMass(800.);

    myWorld->addObject(this->myVehicle);
    this->myVehicle->printInfo();

    //
    osgUtil::SmoothingVisitor *sv=new osgUtil::SmoothingVisitor;
    myVehicle->pNode->accept(*sv);


    osg::StateSet* stateSet2=myWorld->scene->getOrCreateStateSet();
    stateSet2->setMode(GL_DEPTH_TEST,osg::StateAttribute::ON);

//    btosgPlane *myRamp=new btosgPlane(btosgVec3(50,50,50)-50*up);
    //修改
    btosgMoonGround *myRamp=new  btosgMoonGround(btosgVec3(50,50,50)-50*up);
    myRamp->setPosition(0.,0.,0.);
    myWorld->addObject( myRamp );
    myRamp->setName("Ramp");
    myRamp->body->setFriction(1000.);
    osg::ref_ptr<osg::Material> matRamp = new osg::Material;
//    matRamp->setAmbient (osg::Material::FRONT_AND_BACK, osg::Vec4(0., 0., 0., 1.0));
//    matRamp->setDiffuse (osg::Material::FRONT_AND_BACK, osg::Vec4(0.7, 0.8, 0.0, 1.0));
//    matRamp->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0, 0, 0, 1.0));
//    matRamp->setShininess(osg::Material::FRONT_AND_BACK, 64);

    matRamp->setAmbient (osg::Material::FRONT_AND_BACK, osg::Vec4(0.3, 0.3, 0.3, 1.0));
    matRamp->setDiffuse (osg::Material::FRONT_AND_BACK, osg::Vec4(0.5, 0.5, 0.5, 1.0));
    matRamp->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1.0));
    matRamp->setShininess(osg::Material::FRONT_AND_BACK, 10);
    myRamp->setMaterial(matRamp);






    osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
    int sunPosx=10000;
    int sunPosy=40000;
    int sunPosz=5000;
    ls->getLight()->setPosition(osg::Vec4(sunPosx,sunPosy,sunPosz,1.));
    // 设置光照属性以模拟太阳光
//    ls->getLight()->setAmbient(osg::Vec4(0.1, 0.1, 0.1, 1.0));  // 弱环境光
//    ls->getLight()->setDiffuse(osg::Vec4(1.0, 1.0, 1.0, 1.0));  // 强漫反射光
//    ls->getLight()->setSpecular(osg::Vec4(0.2, 0.2, 0.2, 1.0)); // 适度镜面反射光
//    myWorld->root->addChild(ls.get());
    ls->getLight()->setPosition(osg::Vec4(2.5,-10+30*up[1],50.*up[2],1.));
    ls->getLight()->setAmbient(osg::Vec4(0.1, 0.1, 0.1, 1.0));
    ls->getLight()->setDiffuse(osg::Vec4(1.0, 1.0, 1.0, 1.0));
    ls->getLight()->setSpecular(osg::Vec4(0.2, 0.2, 0.2, 1.0));
    myWorld->scene->addChild(ls.get());

//    osg::Vec3d eye(osg::Vec3(0., -5. , -5.)+up*50.);
    osg::Vec3d eye(osg::Vec3(0., 0. , 0.)+up*50.);
    osg::Vec3d center(0., 0., 0.);
    manipulator=new osgGA::TrackballManipulator;
    // Make sure that OSG is not overriding our home position
    manipulator->setAutoComputeHomePosition(false);
    // Set the desired home position of the Trackball Manipulator
    manipulator->setHomePosition(eye,center,up);
    // Force the camera to move to the home position
    manipulator->home(0.0);

    osg::Vec3d eye1(osg::Vec3(0.,25., 10.));
    osg::Vec3d center1(0., 0., 0.);
    manipulator1 = new osgGA::NodeTrackerManipulator;
//        manipulator->setHomePosition( osg::Vec3(0, -10.0, 0), osg::Vec3(), osg::Z_AXIS );
    manipulator1->setHomePosition( eye1, center1, up );
    manipulator1->setTrackerMode( osgGA::NodeTrackerManipulator::NODE_CENTER_AND_AZIM );
    manipulator1->setRotationMode( osgGA::NodeTrackerManipulator::TRACKBALL );
    manipulator1->setTrackNode( myVehicle->pNode );




    MyContactResultCallback callback;
    callback.m_collisionDetected = false;
    myWorld->dynamic->contactTest(this->myVehicle->body, callback);











    // 如果发生碰撞，则在这里执行相应的操作
    if (callback.m_collisionDetected)
    {
        qDebug()<<"collision happen";
    }




}

GameWorld::~GameWorld(){
    delete this->myWorld;
    delete this->myVehicle;
    delete this->viewer;

}

GameWorld *GameWorld::getHandle(){
    if(!_obj)
        _obj = new GameWorld;
    return _obj;
}

void GameWorld::setViewer(osgViewer::Viewer* _viewer)
{
    viewer = _viewer;

}

void GameWorld::update()
{

}









